<?php
declare (strict_types=1);

namespace app\command;

use Swoole\Coroutine;
use Swoole\Coroutine\Http\Client;
use think\console\Command;
use think\console\Input;
use think\console\input\Argument;
use think\console\input\Option;
use think\console\Output;

class WebSocketClient extends Command
{
    protected function configure()
    {
        // 指令配置
        $this->setName('start_websocket_client')
            ->setDescription('the app\command\websocketclient command');
    }

    protected function execute(Input $input, Output $output)
    {
        // 指令输出
        $output->writeln('app\command\websocketclient');
        Coroutine::create(function () {
            $client = new Client('127.0.0.1', 9502);
            $ret = $client->upgrade('/?authorization=' . config('common.webSocketClientAuthorization'));
            if ($ret) {
                $client->push(json_encode([
                    'action' => 'fresh',
                    'serialNumber' => '',
                    'data' => [
                        'name' => 'zhangsan',
                        'age' => 18
                    ]
                ], 320));
                //$response = $client->recv();
            }
        });
    }
}
